package com.example.wc.logindemolistview;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginDemoActivity extends AppCompatActivity{

    private EditText mUser;
    private EditText mPassword;
    private TextView mLoginBtn;
    private TextView mRegisterTextviewLogin;//还没有帐号？立即注册

    public static String ACCESS = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.CustomTitleBarTheme);
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_login_demo);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.login_title_bar);

        mUser = (EditText)findViewById(R.id.user_edittext_login);
        mPassword = (EditText)findViewById(R.id.password_edittext_login);
        mLoginBtn = (TextView)findViewById(R.id.login_button_login);
        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent i = new Intent(LoginDemoActivity.this, HomepageActivity.class);
//                startActivity(i);

                JSONObject params = new JSONObject();
                try {
                    params.put("mobile","15736723671");
                    params.put("password","e10adc3949ba59abbe56e057f20f883e");
//                    params.put("mobile",mUser.getText().toString());
//                    params.put("password",mUsers.getPassword());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.diyerland.com/user/api/login", params,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                if(response == null){
                                    return;
                                }
                                try {
                                    Integer error = (Integer) response.get("error");
                                    if(error == 0){
                                        JSONObject data = response.getJSONObject("data");
                                        String mobile = data.getString("mobile");
                                        String accessToken = data.getString("accessToken");
                                        ACCESS = accessToken;
                                        Intent i = new Intent(LoginDemoActivity.this, HomepageActivity.class);
//                                        Intent i = new Intent(LoginDemoActivity.this, BlogsListActivity.class);
                                        startActivity(i);
//                                        Toast.makeText(LoginDemoActivity.this,accessToken,Toast.LENGTH_SHORT).show();
                                    }
                                }catch (Exception e){

                                    Toast.makeText(LoginDemoActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(LoginDemoActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show();
                    }
                });
                Volley.newRequestQueue(LoginDemoActivity.this).add(jsonObjectRequest);

            }
        });

        mRegisterTextviewLogin=(TextView)this.findViewById(R.id.register_textview_login);
        setForegroundColorSpan();
        mRegisterTextviewLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginDemoActivity.this, RegisterDemoRegister.class);
                startActivity(i);
            }
        });

    }

    //登录页面-立即注册自定义颜色
    private void setForegroundColorSpan() {
        SpannableString spanString = new SpannableString(this.getString(R.string.register_login));
        ForegroundColorSpan span = new ForegroundColorSpan(Color.rgb(255,85,93));
        spanString.setSpan(span, 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mRegisterTextviewLogin.setText(spanString);
    }




}
